package com.ustadzmenjawab.crawler.en;

import java.io.IOException;
import java.util.Set;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.google.gwt.dev.util.collect.HashSet;
import com.ustadzmenjawab.crawler.CrawlerRoot;
import com.ustadzmenjawab.shared.Article;

public class IslamQAEnglishCrawler extends CrawlerRoot {
	public static final String HOME_URL = "http://islamqa.com/en/";
	public static final String PATTERN = "islamqa.com/en/";
	@Override
	public boolean isRelevantURL(String url) {
		if(url.contains("/mail")
				||url.contains("/print")
				||url.contains("/doc")
				||url.contains("/pdf")
				||url.contains("#")
				||url.contains(".xml")
				||url.contains("/bayan")
				||url.contains("/add")
				||url.contains("/islamqapages")
				||url.contains("addthis.com")
				||url.contains("mailto")
				||url.contains("/pda")
				||url.contains("/en/en/")
		){
			return false;
		}
		if(url.contains(PATTERN))
			return true;
		return false;
	}

	@Override
	public boolean isRelevantPage(String content) {
		if(content.contains("qtitle") && content.contains("question") && content.contains("answer")){
			return true;
		}
		return false;
	}

	@Override
	public String getTitle(String body) {
		Document doc = Jsoup.parse(body);
		Element p = doc.select("div[class*=qtitle]").first();
		return p.text();
	}

	@Override
	public String getQuestion(String body) {
		Document doc = Jsoup.parse(body);
		Element p = doc.select("span[class*=question]").first();
		return p.text();
	}

	@Override
	public String getArticle(String body) {
		Document doc = Jsoup.parse(body);
		Element p = doc.select("span[class*=question]").first();
		String question = p.text();
		p = doc.select("div[class*=answer]").first();
		
		String answer = p.text();
		return question + " " +answer;
	}

	@Override
	public boolean insertContent(String url, String content) {
		try{
			Article article = new Article();
			String title = getTitle(content);
			if(isTitleExist(title, "islamqa.com")){
				return false;
			}
			article.setJudul(title);
			article.setKategori("shariah");
			article.setSumber("islamqa.com");
			article.setTahun("2011");
			article.setUrl(url);
			article.setUstadz("Shaykh Muhammad Sholih Al-Munajid");
			article.setArtikel(getArticle(content));
			article.setPertanyaan(getQuestion(content));
			db.insertArticle(article);
			return true;
		}catch(Exception e){
			System.out.println(" exception "+e.getClass() +" :" + e.getMessage());
			e.printStackTrace();
			insertFailure(url,"exception "+e.getClass() +" :" + e.getMessage());
			return false;
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			IslamQAEnglishCrawler cr = new IslamQAEnglishCrawler();

			Document doc = Jsoup.connect(HOME_URL).timeout(0).get();
			Elements unvisited = doc.select("a[href]");
			Set<String> visited = new HashSet<String>();
			while(unvisited.size() > 0){
				Element link = unvisited.first();
				//for(Element link:  articlesLs){
					try{
						String absHref = link.attr("abs:href");
						unvisited.remove(0);
						if(!visited.contains(absHref.toLowerCase())){
							visited.add(absHref.toLowerCase());
							if(cr.isRelevantURL(absHref)){
								Document page = Jsoup.connect(absHref).timeout(0).get();
								String content = page.html();
								Elements pageLinks = page.select("a[href]");
								for(Element newlink : pageLinks){
									String newLinkStr = newlink.attr("abs:href").toLowerCase();
									if(!visited.contains(newLinkStr) && cr.isRelevantURL(newLinkStr)){
										System.out.println(newLinkStr);
										unvisited.add(newlink);
									}
								}
								if(cr.isRelevantPage(content)){
									System.out.println(absHref + " is a relevant page");
									
									if(!cr.isUrlExist(absHref)){
										cr.insertContent(absHref, content);
									}
								}else{
									System.out.println(absHref + " is not a relevant page");
								}
							}
						}
					}catch(Exception e){
						System.out.println(" exception "+e.getClass() +" :" + e.getMessage());
						e.printStackTrace();
					}
				}
			//}
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}

}
